ARCH := $(shell uname -m)

CXX = g++
LDFLAGS = -lhdf5_cpp -lhdf5
SRCS = src/config_parser.cpp src/datasets.cpp src/test_result.cpp src/main.cpp
FAISSROOT = /usr/local                # 请根据实际路径修改
FAISSSO = faiss
HNSWLIBROOT = /usr/local/hnswlib/ # 请根据实际路径修改
SRARECALLROOT = /usr/local/sra_recall # 请根据实际路径修改
CXXFLAGS = -std=c++17 -fPIC -Ofast -Iinclude -I$(strip ${FAISSROOT})/include -fopenmp
ifdef KRL_PATH
	LDFLAGS += -L${KRL_PATH}/lib -lkrl
	CXXFLAGS += -I${KRL_PATH}/include
endif

ifeq ($(ARCH),aarch64)
	CXXFLAGS += -march=armv8.2-a+dotprod
else
	ONEAPIROOT = /opt/intel/oneapi    # 请根据实际路径修改
	CXXFLAGS += -m64 -DMKL_ILP64  -I$(strip ${ONEAPIROOT})/mkl/2025.0/include
	LDFLAGS += -L$(strip ${ONEAPIROOT})/mkl/2025.0/lib/ -L$(strip ${ONEAPIROOT})/compiler/2025.0/lib/ -Wl,--no-as-needed -lmkl_intel_lp64 -lmkl_gnu_thread -lmkl_core -lgomp -lpthread -lm -ldl
	FAISSSO = faiss_avx512
endif

ifeq ($(MAKECMDGOALS), kbest_test)
	TARGET = kbest_test
	DEFINES = -DKBEST_TEST
	CXXFLAGS += -I$(strip ${SRARECALLROOT})/include
	LDFLAGS += -L$(strip ${SRARECALLROOT})/lib -lkbest -lnuma
	SRCS += src/kbest_index.cpp
else ifeq ($(MAKECMDGOALS), hnsw_test)
	TARGET = hnsw_test
	DEFINES = -DHNSW_TEST
	LDFLAGS += -L$(strip ${FAISSROOT})/lib64 -l${FAISSSO}
	SRCS += src/faiss_hnsw_index.cpp
else ifeq ($(MAKECMDGOALS), pqfs_test)
	TARGET = pqfs_test
	DEFINES = -DPQFS_TEST
	LDFLAGS += -L$(strip ${FAISSROOT})/lib64 -l${FAISSSO}
	SRCS += src/faiss_pqfs_index.cpp
else ifeq ($(MAKECMDGOALS), ivfpq_test)
	TARGET = ivfpq_test
	DEFINES = -DIVFPQ_TEST
	LDFLAGS += -L$(strip ${FAISSROOT})/lib64 -l${FAISSSO}
	SRCS += src/faiss_ivfpq_index.cpp
else ifeq ($(MAKECMDGOALS), ivfpqfs_test)
	TARGET = ivfpqfs_test
	DEFINES = -DIVFPQFS_TEST
	LDFLAGS += -L$(strip ${FAISSROOT})/lib64 -l${FAISSSO}
	SRCS += src/faiss_ivfpqfs_index.cpp
else ifeq ($(MAKECMDGOALS), ivfflat_test)
	TARGET = ivfflat_test
	DEFINES = -DIVFFLAT_TEST
	LDFLAGS += -L$(strip ${FAISSROOT})/lib64 -l${FAISSSO}
	SRCS += src/faiss_ivfflat_index.cpp
else ifeq ($(MAKECMDGOALS), hnswlib_test)
	TARGET = hnswlib_test
	DEFINES = -DHNSWLIB_TEST
	CXXFLAGS += -I$(strip ${HNSWLIBROOT})
	SRCS += src/hnswlib_index.cpp
endif

$(TARGET):
	$(CXX) $(CXXFLAGS) $(SRCS) $(LDFLAGS) $(DEFINES) -o $@

